package com.dating.base.module;

import java.util.Date;
import java.util.List;

import org.apache.shiro.util.ByteSource;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.lang.Lang;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.dating.base.bean.Role;
import com.dating.base.bean.User;
import com.dating.base.bean.User.Status;
import com.dating.base.bean.User.Type;
import com.dating.base.bean.UserRole;
import com.dating.base.dto.Pager;
import com.dating.base.service.RoleService;
import com.dating.base.service.ShiroUserService;
import com.dating.base.service.UserService;
import com.dating.core.entries.Result;
import com.dating.core.ext.BaseModule;
import com.dating.utils.StrKit;

public class UserModule extends BaseModule{

	@Inject
	private UserService userService;
	
	@Inject
	private RoleService roleService;

	@Inject
	private ShiroUserService shiroUserService;

	
	@Override
	public String _getNameSpace() {
		// TODO Auto-generated method stub
		return "user";
	}
	
	/**
	 * 用户列表
	 * @return
	 */
	@At("user/list")
	@Ok("beetl:templates/pages/base/user_list.html")
	public Result toList(){
		Pager pager = createPager("key");
		userService.getUsersByPage(pager);
		return Result.success().addData("pager", pager);
	}
	
	@At("user/getList")
	@Ok("json")
	public Result getList(){
		Pager pager = createPager("key");
		userService.getUsersByPage(pager);
		return Result.success().addData("pager", pager);
	}
	
	
	
	/**
	 * to 用户编辑
	 * @param id
	 * @return
	 */
	@At("user/edit")
	@Ok("beetl:templates/pages/base/user_edit.html")
	public Result toEdit(Integer id){
		User user = null ;
		if(id != null){
			user = userService.getById(id);
		}
		List<Role> roles = roleService.getAllRoles();
		shiroUserService.findRolesInfo(1, Type.PLATFORM);
		return Result.success().addData("user", user).addData("roles", roles);
	}
	
	
	/**
	 *  
	 * @return
	 */
	@At("user/setting")
	@Ok("beetl:pages/base/user_setting.html")
	public Result toSetting(){
		return Result.success();
	}
	
	
	/**
	 * AdaptBy(type = UploadAdaptor.class, args = { "${app.root}/WEB-INF/tmp" })
	 * @return
	 */
	@At("user/editSave")
	@Ok("json")
	public Result editSave(@Param("..") User user){
		try {
			User checkUser = userService.getUser(user.getName());
			if(checkUser != null && !checkUser.getId().equals(user.getId())){
				return Result.fail("用户名存在重复！");
			}
			log.debug("statusSwitch:" + getPara("statusSwitch"));
			if(StrKit.notBlank(getPara("statusSwitch"))){
				user.setStatus(Status.ACTIVED);
			}else{
				user.setStatus(Status.DISABLED);
			}
			if(user.getId() == null){
				user.setCreateTime(new Date());
				user.setPassword(Lang.md5("123456" + ByteSource.Util.bytes(user.getName())));
				user.setUserType(Type.PLATFORM);
				userService.addUser(user);
				log.info(String.format("添加的用户[%s] : %s , %s, %s ",  user.getName() ,user.getId(), user.getRealName(), user.getPhone()));
			}else{
				userService.updateUser(user);
			}
		} catch (Exception e) {
			log.error("添加用户失败！", e);
			return Result.fail("添加用户失败！");
		}
		return Result.success();
	}
	
	
	@At("user/del")
	@Ok("json")
	public Result del(){
		Integer id = getParaToInteger("id");
		if(roleService.isAdmin(id, Type.PLATFORM)){
			return Result.fail("不能删除该用户！");
		}
		try {
			userService.delUser(id);
		} catch (Exception e) {
			return Result.fail("删除用户失败！");
		}
		return Result.success();
	}
	
	@At("user/setRole")
	@Ok("json")
	public Result userRoles(Integer uid, Integer rid, boolean op){
		UserRole userRole = new UserRole(uid, rid, Type.PLATFORM);
		try {
			userService.setUserRole(userRole, op);
		} catch (Exception e) {
			log.debug("设置用户角色失败！", e);
			return Result.fail("设置用户角色失败！");
		}
		return Result.success();
	}
}
